package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.User;
import com.digital.welcome.entity.UserUse;
import com.digital.welcome.mapper.UserUseMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.UserUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 莲童的雨儿
 */
@Service
public class UserUseServiceImpl extends ServiceImpl<UserUseMapper, UserUse> implements UserUseService {
    @Autowired
    private UserUseMapper userUseMapper;

    @Override
    public Boolean selectState(String userName) {
        QueryWrapper<UserUse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        Integer count = userUseMapper.selectCount(queryWrapper);
        return count != 0;
    }

    @Override
    public String userUseDo(User user) {
        if (user == null || user.getUserName() == null) {
            return JSON.toJSONString(new Result<>().setCode(0).setMessage("用户名为空"));
        }
        UserUse userUse = new UserUse();
        userUse.setUserName(user.getUserName());
        userUseMapper.insert(userUse);
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("生活用品办理成功"));
    }
}
